<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            position: absolute;
            left: 0px;
            top: 0px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <div id="demo"></div>

    <script>
        var oDiv = document.getElementById('demo');
        oDiv.onclick = function() {
            startMove(this);
        }

        function startMove(dom) {
            clearInterval(dom.timer);
            var speedX = 6;
            var speedY = 8;
            var g = 3;
            dom.timer = setInterval(function(){
                speedY = speedY + g;
                var newLeft = dom.offsetLeft + speedX;
                var newTop = dom.offsetTop + speedY;
                if(newTop + dom.offsetHeight >= window.innerHeight) {
                    newTop = window.innerHeight - dom.offsetHeight;
                    speedY = -speedY;
                    speedY *= 0.8
                    speedX *= 0.8
                }
                if(newLeft + dom.offsetWidth >= window.innerWidth){
                    newLeft = window.innerWidth - dom.offsetWidth;
                    speedX = - speedX;
                    speedY *= 0.8
                    speedX *= 0.8
                }
                if(newTop <= 0) {
                    newTop = 0;
                    speedY = -speedY;
                    speedY *= 0.8
                    speedX *= 0.8
                }
                if(newLeft <= 0) {
                    newLeft = 0;
                    speedX = - speedX;
                    speedY *= 0.8
                    speedX *= 0.8
                }
                // console.log(speedX, speedY);
                if(Math.abs(speedX) < 1) {
                    speedX = 0;
                }
                if(Math.abs(speedY) < 1) {
                    speedY = 0;
                }
                if(speedX == 0 && speedY == 0 && newTop == window.innerHeight - dom.offsetHeight) {
                    clearInterval(dom.timer);
                    console.log('over');
                }else{
                    dom.style.left = newLeft + 'px';
                    dom.style.top = newTop + 'px';
                }
            }, 30);
        }
    </script>
</body>
</html>